package com.ll.menu.user.order;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.droid.library.app.App;
import com.droid.library.app.emptyview.BaseCallback;
import com.droid.library.app.emptyview.BaseEmptyViewManager;
import com.droid.library.app.emptyview.CallbackAdapter;
import com.droid.library.app.mvp.BaseMVPFragment;
import com.droid.library.app.mvp.IUI;
import com.droid.library.app.mvp.list.FooterManager;
import com.droid.library.widget.recyclerview.BaseRecyclerAdapter;
import com.droid.library.widget.recyclerview.RefreshableRecyclerView;
import com.ll.menu.user.R;
import com.ll.menu.user.app.entity.Food;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 已点
 *
 * @author wangheng
 */

public class OrderFragment extends BaseMVPFragment<OrderPresenter> implements IOrderUI,View.OnClickListener {

    @InjectView(R.id.flOrderRoot)
    ViewGroup mEmptyViewContainer;

    @InjectView(R.id.rvOrder)
    RefreshableRecyclerView mRecyclerView;

    @InjectView(R.id.tvOrderTotalPrice)
    TextView mTotalPriceTextView;

    private OrderEmptyViewManager mEmptyViewManager;
    private OrderAdapter mAdapter;

    private String mTotalPriceFormat = null;

    @Override
    protected View onCreateViewExecute(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order,container,false);

        ButterKnife.inject(OrderFragment.this,view);

        mTotalPriceFormat = App.getInstance().getString(R.string.total_price_mode);

        initView(view);
        getPresenter().requestList();

        return view;
    }

    @Override
    public void initView(View view){
        mEmptyViewManager = new OrderEmptyViewManager(new CallbackAdapter());
        mEmptyViewManager.addEmptyViewToFirst(getActivity(), mEmptyViewContainer);
        mEmptyViewManager.hideAllEmptyView();

        mAdapter = new OrderAdapter(OrderFragment.this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public BaseRecyclerAdapter<Food> getAdapter() {
        return mAdapter;
    }

    @Override
    public void onPullDownRefreshCompleted() {

    }

    @Override
    public void onPullUpRefreshCompleted() {
        mRecyclerView.setIsLoading(false);
    }

    @Override
    public FooterManager getFooterManager() {
        return null;
    }

    @Override
    public BaseEmptyViewManager<? extends BaseCallback> getEmptyViewManager() {
        return mEmptyViewManager;
    }

    @Override
    protected IUI getUI() {
        return OrderFragment.this;
    }

    @Override
    protected OrderPresenter createPresenter() {
        return new OrderPresenter();
    }


    @OnClick({R.id.tvOrderCheck})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvOrderCheck:

                break;
            default:
                break;
        }
    }

    private static final String FRAGMENT_TAG = "OrderFragment";

    public static OrderFragment getInstance(FragmentManager fm) {
        Fragment fragment = fm.findFragmentByTag(FRAGMENT_TAG);
        if (fragment == null || !(fragment instanceof OrderFragment)) {
            return new OrderFragment();
        }

        return (OrderFragment) fragment;
    }

    @Override
    public void setTotalPrice(float price) {
        if(isFragmentDetached() || mTotalPriceTextView == null){
            return;
        }
        mTotalPriceTextView.setText(String.format(mTotalPriceFormat,price));
    }
}
